package info.hexin.dao.core;

import info.hexin.dao.callback.ConnectionCallback;
import info.hexin.lang.Exceptions;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

public class Dao extends BaseDao {

    public Dao(DataSource dataSource) {
        super(dataSource);
    }

    public <T> T insert(T t) {
        if (t instanceof String) {
            // 为sql语句
            exec(new ConnectionCallback() {
                @Override
                public Object doInConnection(Connection conn) {
                    return null;
                }
            });
        }
        return t;
    }

    public void execSql(final String sql, final List<Object> args) {
        exec(new ConnectionCallback() {
            @Override
            public Object doInConnection(Connection conn) throws SQLException {
                PreparedStatement statement = conn.prepareStatement(sql);
                for (Object o : args) {

                }
                return null;
            }
        });
    }

    public void executeTranscation(Connection conn, Atom atom) {
        try {
            boolean oldIsAuotCommit = conn.getAutoCommit();
            conn.setAutoCommit(false);
            atom.run(conn);
            conn.commit();
            conn.setAutoCommit(oldIsAuotCommit);
        } catch (SQLException e) {
            Exceptions.make(e);
        }
    }
}
